#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/3/27 20:22
# @Author  : Rem~
# @File    : 002IdealSineSignal.py
# @function: 5MHz sine信号，无偏置，无噪声
# 模拟探测器输出经过调理电路后，最理想的纯交流信号

import numpy as np
import matplotlib.pyplot as plt

# sample frequency
fs = 100
# signal frequency
f = 5
# signal phase
fai = 45*np.pi/180
# sample point
N = 1000
# x axis
x = np.linspace(0, N-1, N)
# amplitude
amp = 2**12
sig = amp*np.cos(2*np.pi*f*(x/fs)+fai)

fig = plt.figure("Time&Frequency Domain Analyse")
axes1 = fig.add_subplot(2, 1, 1)
axes2 = fig.add_subplot(2, 2, 3)
axes3 = fig.add_subplot(2, 2, 4)
axes1.plot(x, sig, "-b")
axes1.set(xlabel="sample point", ylabel="signal amplitude(LSB)",
          title="Time domain signal")
axes1.grid(True)

sig_fft = np.fft.fft(sig)
sig_fft_magnitude = np.abs(sig_fft)/N*2
sig_fft_phase = np.angle(sig_fft)*(180/np.pi)

axes2.stem(x[0:int(N/2)-1]*(fs/N), sig_fft_magnitude[0:int(N/2)-1])
axes2.set(xlabel="frequency axis(MHz)", ylabel="signal amplitude(LSB)")
axes2.grid(True)

axes3.plot(x[0:int(N/2)-1]*(fs/N), sig_fft_phase[0:int(N/2)-1])
axes3.set(xlabel="frequency axis(MHz)", ylabel="signal phase(°)")
axes3.grid(True)

sig_fft_abs_max = np.max(sig_fft_magnitude)
sig_fft_abs_max_f = np.argmax(sig_fft_magnitude[0:int(N/2)-1])
sig_fft_max_f_phase = sig_fft_phase[sig_fft_abs_max_f]
print('频谱峰值:', sig_fft_abs_max, '\n最大峰值点:', sig_fft_abs_max_f*(fs/N), 'MHz',
      '\n最大频率点相位', sig_fft_max_f_phase, '°')

fig.tight_layout()
# show放print前面时，console界面不会出现相应的print信息
plt.show()
